<template>
	<view class="container">
		<u-tabs :list="list1" :scrollable="false"  :inactiveStyle="{
				fontSize: '30rpx',
				fontWeight: '400',
				color: 'rgba(0,0,0,0.5)',
			}" :activeStyle="{
				fontSize: '32rpx',
				fontWeight: '600',
				color: '#333',
			}" :itemStyle="{width: '14%', height: '44px'}" lineWidth="20rpx" lineColor="rgba(0,0,0,0.5)" @change="selectnav">
		</u-tabs>

		<scroll-view scroll-y="true" class="scroll-box" @scrolltolower="lower">
			
			<view class="list" v-for="(item,index) in list" :key="index">
				<view class="title flex f-x-b">
					<view class="text ellipsis">{{item.name}}</view>
				
					<view class="status" v-if="item.state === 0">
						待审核
					</view>
					<view class="status" v-else-if="item.state === 1">
						上架中
					</view>
					<view class="status" v-else-if="item.state === 2">
						已下架
					</view>
					<view class="status" v-else-if="item.state === -1">
						已拒绝
					</view>
				</view>
				<view class="subtitle ellipsis">{{item.content}}</view>
				<!-- <view class="images flex f-w f-x-b" v-if="item.images.length>0">
					<image class="image" :src="oss(item.images[0])" mode="aspectFill"></image>
				</view> -->
				<view class="fun">
					<view class="fun_item edit" @click="edit(item)">
						<image class="image" src="https://www.stck.cc/myPurchase/edit.png" mode=""></image>
						<view class="name">
							编辑
						</view>
					</view>
					<view class="fun_item up" v-if="item.state === 2" @click="up(item)">
						<image class="image" src="https://www.stck.cc/myPurchase/add.png" mode=""></image>
						<view class="name">
							上架
						</view>
					</view>
					<view class="fun_item down" v-if="item.state === 1" @click="down(item)">
						<image class="image" src="https://www.stck.cc/myPurchase/close.png" mode=""></image>
						<view class="name">
							下架
						</view>
					</view>
					<view class="fun_item del" @click="dels(item,index)">
						<image class="image" src="https://www.stck.cc/myPurchase/del.png" mode=""></image>
						<view class="name">
							删除
						</view>
					</view>
				</view>
			</view>
			<u-empty v-if="list.length == 0 && finish" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
			</u-empty>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list1: [{
						type: 0,
						name: '待审核',
					}, {
						type: 1,
						name: '上架中'
					},
					{
						type: 2,
						name: '已下架'
					}, {
						type: -1,
						name: '已拒绝'
					}
				],
				list: [],
				type: 0,
				page: 1,
				finish: false
			}
		},
		onLoad() {
			this.barHeight = uni.getSystemInfoSync().statusBarHeight;
			this.getlist()
			uni.$on('edit',()=>{
				console.log(111)
				this.list = []
				this.page = 1
				this.finish = false
				this.getlist()
			})
		},
		methods: {
			selectnav(e){
				console.log(e)
				this.type = e.type
				this.list = []
				this.page = 1
				this.finish = false
				this.getlist()
			},
			// 跳转详情
			godetail(item){
				if(item.state == 1){
					uni.$u.route({
						url: 'pagesA/index/index_detail',
						params: {
							id: item.id
						}
					})
				}else if(item.state == 0){
					this.toast('正在审核，请稍等')
				}else if(item.state == 2) {
					this.toast('该商品已下架')
				}else if(item.state == -1){
					this.toast('该商品审核失败，请重新上传')
				}
			},
			up(item){
				uni.$u.http.post('/postbuy', {
					type: 'up',
					id: item.id
				}).then(res => {
					if (res.code == 1) {
						this.toast(res.msg)
						setTimeout(() => {
							this.list = []
							this.page = 1
							this.finisn = false
							this.getlist()
						}, 500)
					}
				}).catch((res) => {
				
				})
			},
			// 编辑
			edit(item) {
				console.log(item)
				uni.$u.route({
					url: 'pagesA/index/upload',
					params: {
						data: JSON.stringify(item),
						type: 1
					}
				})
			},
			// 下架
			down(item) {
				uni.$u.http.post('/postbuy', {
					type: 'lower',
					id: item.id
				}).then(res => {
					if (res.code == 1) {
						this.toast(res.msg)
						setTimeout(() => {
							this.list = []
							this.page = 1
							this.finisn = false
							this.getlist()
						}, 500)
					}
				}).catch((res) => {
			
				})
			},
			// 删除
			dels(item,index) {
				console.log(item)
				console.log(index)
				let that = this
				uni.showModal({
					title: '温馨提示',
					content: '确定要删除该信息吗？',
					success: function (res) {
						if (res.confirm) {
							uni.$u.http.post('/postbuy', {
								type: 'del',
								id: item.id
							}).then(res => {
								if (res.code == 1) {
									that.toast(res.msg)
									setTimeout(() => {
										that.list = []
										that.page = 1
										that.finisn = false
										that.getlist()
									}, 500)
								}
							}).catch((res) => {
										
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			// 刷新
			refresh(item) {
				this.id = item.id
				uni.$u.http.post('/refreshnum').then(res => {
					if (res.code == 1) {
						this.contenta = res.data
						this.showa = true
					}
				}).catch((res) => {
				
				})
			},
			see(item) {
				this.content = item.note
				this.show = true
			},
			
			lower() {
				this.page++
				this.getlist()
			},
			getlist() {
				if (this.finish) {
					return
				}
			
				uni.$u.http.post('/buylist', {
					type: this.type,
					page: this.page
				}).then(res => {
					if (res.code == 1) {
			
						this.list = this.list.concat(res.data)
						if (res.data.length === 0) {
							this.finish = true
						}
			
					}
				}).catch((res) => {
			
				})
			},
			// 查看失败原因
			confirm() {
				this.show = false
			},
			confirma() {
				uni.$u.http.post('/postfree', {
					type: 'refresh',
					id: this.id
				}).then(res => {
					if (res.code == 1) {
						this.toast(res.msg)
						setTimeout(() => {
							this.list = []
							this.page = 1
							this.finisn = false
							this.getlist()
						}, 500)
						this.showa = false
					}
				}).catch((res) => {
				
				})
				console.log(111)
			},
			cancela() {
				this.showa = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		display: flex;
		flex-direction: column;

		/deep/ .u-tabs__wrapper__nav__item__text {
			font-size: 17px;
		}


		.nav {
			margin: 0 30rpx;
			padding-bottom: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;

			.back {
				width: 40rpx;
				height: 40rpx;
			}

			.nav_name {
				flex: 1;
				margin-right: 30rpx;
				text-align: center;
				font-size: 36rpx;
				font-weight: bold;
				line-height: 40rpx;
			}
		}

		.tab {
			display: flex;
			justify-content: center;
		}

		.scroll-box {
			margin-top: 40rpx;
			flex: 1;
			overflow: hidden;

			.list {
				margin: 0 32rpx 60rpx;
				padding-bottom: 30rpx;
				border-bottom: 1rpx solid #eaeaea;

				.title {
					margin-bottom: 20rpx;

					.text {
						font-size: 32rpx;
						font-weight: 600;
						color: rgba(0, 0, 0, 0.8);
					}

					.status {
						font-size: 28rpx;
						font-weight: 400;
						&.up {
							color: #FEBF4E;
						}
					}
				}

				.subtitle {
					font-size: 28rpx;
					font-weight: 400;
					color: rgba(0, 0, 0, 0.5);
					margin-bottom: 20rpx;
				}

				.images {
					.image {
						width: 219rpx;
						height: 175rpx;
						background: #D7D7D7;
						border-radius: 32rpx;
					}
				}

				.fun {
					display: flex;
					margin-top: 30rpx;
					justify-content: space-between;

					.fun_item {
						width: 218rpx;
						display: flex;
						justify-content: center;
						align-items: center;

						image {
							width: 30rpx;
							height: 30rpx;
						}

						.name {
							font-size: 28rpx;
							font-weight: 400;
							color: #3D3D3D;
							margin-left: 15rpx;
						}
					}
				}
			}
		}
	}
</style>
